﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Config;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Collections.ObjectModel;
using log4net;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Content;

namespace ThreeByte.ActiveDeck.AddIn.Controls.Wizard.Pages
{
    /// <summary>
    /// Interaction logic for CreateContentPage.xaml
    /// </summary>
    public partial class CreateContentPage : Page, INotifyPropertyChanged
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(CreateContentPage));
        private readonly CoreConfig Config;

        public CreateContentPage() {
            InitializeComponent();

            Config = WizardMainFrame.ActiveConfig;

            //Construct thumbnails from all of the presentation slides
            Slides = new Collection<PowerPoint.Slide>();

            foreach(PowerPoint.Slide s in Config.Presentation.Slides) {
                Slides.Add(s);
            }

            if(Slides.Count > 0) {
                CurrentSlideIndex = 0;
                DisplaySlide(Slides[CurrentSlideIndex]);
            }
            UpdateButtons();
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public PowerPoint.Slide _currentPreviewSlide;
        public PowerPoint.Slide CurrentPreviewSlide {
            get {
                return _currentPreviewSlide;
            }
            set {
                _currentPreviewSlide = value;
                NotifyPropertyChanged("CurrentPreviewSlide");
            }
        }

        private void DisplaySlide(PowerPoint.Slide slide) {
            try {

                SlidePreview.Source = SlideUtil.GetPreview(slide);
                CurrentPreviewSlide = slide;

                PowerPointSlideNumberText.Text = string.Format("PowerPoint Slide {0}", slide.SlideNumber);

                DisplayiPadForSlide(slide);

            } catch(Exception ex) {
                log.Warn("Cannot export slide", ex);
            }
        }

        private void DisplayiPadForSlide(PowerPoint.Slide slide) {
            //Look up the slide in the core config and display any preview that exists
            if(Config.ContainsId(slide.SlideID)) {
                SlideConfig sConfig = Config[slide.SlideID];

                //Grab the first PageConfig and load it up
                PageConfig firstPage = sConfig.Pages.First();

                PreviewRoot.Content = PagePreviewFactory.Create(firstPage);

            } else {
                //Show blank iPad
                PreviewRoot.Content = null;
            }

        }


        private Collection<PowerPoint.Slide> Slides;
        private int CurrentSlideIndex;

        private void NextButton_Click(object sender, RoutedEventArgs e) {
            NavigationService navService = this.NavigationService;
            if(navService != null) {
                navService.Navigate(new EnterQuestionPage());
            }
        }

        private void CannotExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = false;
            e.Handled = true;
        }

        private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) {

        }

        private void PrevSlideButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentSlideIndex > 0) {
                CurrentSlideIndex = CurrentSlideIndex - 1;
                DisplaySlide(Slides[CurrentSlideIndex]);
            }
            UpdateButtons();
        }

        private void NextSlideButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentSlideIndex < Slides.Count - 1) {
                CurrentSlideIndex = CurrentSlideIndex + 1;
                DisplaySlide(Slides[CurrentSlideIndex]);
            }
            UpdateButtons();
        }

        private void UpdateButtons() {
            NextSlideButton.IsEnabled = (CurrentSlideIndex < Slides.Count - 1);
            PrevSlideButton.IsEnabled = (CurrentSlideIndex > 0);
        }


        private void MirrorButton_Click(object sender, RoutedEventArgs e) {
            //Take the current slide, export at high resolution and then add to the iPad config.
            PowerPoint.Slide slideToExport = CurrentPreviewSlide;
            if(slideToExport == null) {
                return; //Cannot export null
            }

            BitmapImage image = SlideUtil.Snapshot(slideToExport);
            string imageName = string.Format("Snapshot_Slide{0:000}",slideToExport.SlideID);

            GraphicResource newResource = new GraphicResource(image, imageName, ImageType.JPEG);
            UserSlideConfig newUserSlide = new UserSlideConfig();
            newUserSlide.Name = string.Format("Mirror_Slide_{0:000}", slideToExport.SlideID);
            newUserSlide.Image = newResource;
            newUserSlide.ScaleToFill = false;

            SlideConfig newSlideConfig = new SlideConfig(slideToExport.SlideID);
            PageConfig defaultPage = newSlideConfig.Pages.First();
            newSlideConfig.ReplacePage(defaultPage, newUserSlide);

            //Insert into CoreConfig
            Config[slideToExport.SlideID] = newSlideConfig;

            //Update on preview slide
            DisplayiPadForSlide(slideToExport);
            WizardMainFrame.ActiveTypeCounter.Refresh();
        }

        
    }
}
